// ==UserScript==
// @name         sinopec - exportKpi
// @namespace    http://tampermonkey.net/
// @version      2025-07-15
// @description  exportKpi
// @author       tako
// @match        https://kpi.dsp.sinopec.com/*
// @grant        GM_cookie
// @run-at       context-menu
// ==/UserScript==

(function () {
    'use strict';

    doWork();

    function doWork() {

        const token = getCookie('Omp-Prod-Access-Token');
        // console.log(token); // 输出特定Cookie的值

        fetch('/kpi-web/api/kpiDefine/selectPage', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json;charset=UTF-8',
                'authorization': `Bearer ${token}`,
            },
            body: JSON.stringify({
                "page": 1,
                "limit": 500,
                "isKpiDefined": "1",
                "kpiNature": "100",
                "businessCodeLike": "A3",
                "appId": "2e6e24852a02416da495186378ce3165",
                "mname": "",
                "type": 1,
                "appName": "中国石化全面预算管理系统（股份）",
                "businessName": "财务管理",
                "kpiTreeCode": "20039"
            }),
        }).then(response => {
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            return response.json();
        }).then(json => {
            if (json.success !== true) {
                throw new Error('Failed to fetch KPI data');
            }
            let records = json.data.records;
            console.log(json.data.records);
            records.forEach(record => {
                console.log(`kpiCode: ${record.kpiCode}, kpiName: ${record.kpiName}, kpiDesc: ${record.kpiDesc}`);
            });
        });
    }

    // 获取特定名称的Cookie值
    function getCookie(name) {
        const value = `; ${document.cookie}`;
        const parts = value.split(`; ${name}=`);
        if (parts.length === 2) return parts.pop().split(';').shift();
    }


})();